#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Smooths spectra to specified FWHM. Modified from a matlab version by C.Clare Worley (itself a modification of "Adjusted Smoothtalk")
import numpy as np
import math

class findsmoospec():

	@staticmethod

	def run(spectrum,fwhm):
		x = spectrum[:,0]
		y = spectrum[:,1]
		
		sig = fwhm/math.sqrt(8*math.log(2))
		x_median=np.median(x)
		a=np.exp(-np.power(x-x_median, 2)/(2*pow(sig,2)))
		
		#Strip off extraneous, basically zero values
		a=a[a>0.00001]
		#Need to normalize the Gaussian
		sum_a=np.sum(a)
		a=a/sum_a
		
		I_convolve_smoo=np.convolve(y,a,'same')
		smoo_spec=np.column_stack((x,I_convolve_smoo))
		return smoo_spec
